package com.ruoyi.project.jxc.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.jxc.service.IJxcMfService;
import com.ruoyi.project.jxc.domain.JxcMf;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysNotice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/jxc/mf")
public class JxcMfController extends BaseController {
    @Autowired
    private IJxcMfService jxcMfService;

    @PreAuthorize("@ss.hasPermi('jxc:mf:list')")
    @GetMapping("/list")
    public TableDataInfo list(JxcMf mf)
    {
        startPage();
        List<JxcMf> jxcMfs = jxcMfService.selectMfList(mf);
        return getDataTable(jxcMfs);
    }

    /**
     * 新增厂家
     */
    @PreAuthorize("@ss.hasPermi('jxc:mf:add')")
    @Log(title = "厂家管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody JxcMf mf)
    {
        if (UserConstants.NOT_UNIQUE.equals(jxcMfService.checkMfNameUnique(mf)))
        {
            return AjaxResult.error("新增厂家'" + mf.getMfName() + "'失败，厂家名称已存在");
        }
        mf.setCreateBy(SecurityUtils.getUsername());
        System.out.println(SecurityUtils.getUsername());
        return toAjax(jxcMfService.insertMf(mf));
    }
    /**
     * 根据部门编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('jxc:mf:edit')")
    @GetMapping(value = "/{mfId}")
    public AjaxResult getInfo(@PathVariable Long mfId)
    {
        return AjaxResult.success(jxcMfService.selectMfById(mfId));
    }

    @PreAuthorize("@ss.hasPermi('jxc:mf:edit')")
    @Log(title = "生产厂家管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody JxcMf mf)
    {
        if (UserConstants.NOT_UNIQUE.equals(jxcMfService.checkMfNameUnique(mf)))
        {
            return AjaxResult.error("修改厂家'" + mf.getMfName() + "'失败，厂家名称已存在");
        }
        mf.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(jxcMfService.updateMf(mf));
    }

    /**
     * 删除部门
     */
    @PreAuthorize("@ss.hasPermi('jxc:mf:remove')")
    @Log(title = "厂家管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{mfId}")
    public AjaxResult remove(@PathVariable Long[] mfId)
    {

        return toAjax(jxcMfService.deleteMfById(mfId));
    }
}
